//
//  ZZView.m
//  DrawLine
//
//  Created by evan on 2017/9/22.
//  Copyright © 2017年 evan. All rights reserved.
//

#import "ZZView.h"
#import "UIView+ColorOfPoint.h"
@implementation ZZView

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    CGPoint location = [[touches anyObject] locationInView:self];
    
    UIColor *color = [self colorOfPoint:location];
    NSArray *colorArr = [ZZView colorForGGB:color];
    if ([colorArr[0] floatValue] <= 30 && [colorArr[1] floatValue] <= 30 && [colorArr[2] floatValue] <=30) {
        return;
    }
    self.clickColor = color;
    
    if (self.viewDidClick) {
        
        self.viewDidClick(color,location);
    }
    
}

// 返回颜色RBG值
+ (NSArray *)colorForGGB:(UIColor *)color {
    
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return @[@(components[0] * 255.0),@(components[1] * 255.0),@(components[2] * 255.0)];
}

- (NSMutableArray *)colors {
    
    if (!_colors) {
        
        
        _colors = [NSMutableArray array];
    }
    return _colors;
}

- (NSString *)image{

    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"YYYY-HH-ss-mm-SSSS";
    NSString *str = [formatter stringFromDate:date];
    NSInteger index = arc4random() % 10000;
    NSString *name = [NSString stringWithFormat:@"%@%ld.png",str,index];

    NSString *path_document = NSHomeDirectory();
    //设置一个图片的存储路径
    NSString *imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",name]];

    NSData *_data = UIImagePNGRepresentation(viewImage);
    if ([_data writeToFile:imagePath atomically:YES]) {

        NSLog(@"写入成功");
    }

    return name;
}

@end
